package com.dmcb.trade.business.crawlers.article;

import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

/**
 * 传送门文章解析
 * Created by Administrator on 2016/6/3.
 */
@Service
public class ChuanSongMenParser extends BaseParser {

    /**
     * 网站URL网址
     *
     * @return 链接
     */
    @Override
    protected String url() {
        return CrawlConstant.CHUANSONGMEN_URL;
    }

    /**
     * 获取标题选择器
     *
     * @return 标题选择器
     */
    @Override
    protected String titleSelect() {
        return "h2#activity-name";
    }

    /**
     * 获取日期选择器
     *
     * @return 发布日期选择器
     */
    @Override
    protected String dateSelect() {
        return "em#post-date";
    }

    /**
     * 获取正文内容选择器
     *
     * @return 正文内容选择器
     */
    @Override
    protected String bodySelect() {
        return "div#js_content";
    }

    /**
     * 获取封面选择器
     *
     * @return 封面选择器
     */
    @Override
    protected String coverSelect() {
        return "div.rich_media_content>section>p>span img[data-src],div#media>img[data-src],div#js_content>p img[data-src],div#js_content>p>span img[data-src],div#js_content>p>span>a img[data-src]";
    }


    @Override
    protected void setAuthorName(Document doc, Article article) {
        String authorName = "";
        Elements elements = doc.select("em.rich_media_meta_text");
        if (elements != null && elements.size() > 1) {
            authorName = text(elements.get(1));
            if (StringUtils.isNotBlank(authorName)) {
                authorName = authorName.trim().replace("编辑", "").replace("作者", "").replace("：", "").replace(":", "");
                article.setAuthorName(authorName);
            }
        }
    }

    @Override
    protected boolean isProxyEnabled() {
        return true;
    }
    @Override
    protected String platform() {
        return "传送门网";
    }
}
